package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.food_message.Menu;

import java.util.ArrayList;
import java.util.List;

public class DailyMenusAdapter extends BaseAdapter {

    private Context mContext;
    List<Menu> mlList = new ArrayList<>();

    public DailyMenusAdapter(Context mContext, List<Menu> mealList) {
        this.mContext = mContext;
        this.mlList = mealList;
    }

    @Override
    public int getCount() {
        return mlList.size();
    }

    @Override
    public Object getItem(int position) {
        return mlList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DailyMenusAdapter.ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.home_day_menushow, null);
            viewHolder = new DailyMenusAdapter.ViewHolder();
            viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.menu_show_img);
            viewHolder.mImageViews = (ImageView) convertView.findViewById(R.id.menu_show_state);
            viewHolder.mTvMealName = (TextView) convertView.findViewById(R.id.menu_show_name);
            viewHolder.mTvMealMoney = (TextView) convertView.findViewById(R.id.menu_show_money);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (DailyMenusAdapter.ViewHolder) convertView.getTag();
        }
        viewHolder.mImageView.setBackgroundResource(mlList.get(position).getmImg());
        viewHolder.mImageViews.setVisibility(mlList.get(position).getmImgState() == 1 ? View.VISIBLE : View.GONE);
        viewHolder.mTvMealName.setText(mlList.get(position).getmName());
        viewHolder.mTvMealMoney.setText(mlList.get(position).getmMoney() + "元/50g");
        return convertView;
    }

    public void clear() {
        mlList.clear();
        notifyDataSetChanged();
    }

    public static class ViewHolder {
        ImageView mImageView;
        TextView mTvMealName;
        TextView mTvMealMoney;
        ImageView mImageViews;
    }
}
